package com.danicsoft.daide.command;

import com.danicsoft.daide.token.StandardToken;
import com.danicsoft.daide.token.TokenGroup;
import com.danicsoft.daide.token.TokenMap;
import com.danicsoft.daide.token.TokenMap.Commands;

/**
 * Implements the SLO command. From Eric Wald's <a
 * href="http://pydip.brainshell.org/docs/syntax.html">protocol specification</a>,
 * v0.14:
 * <h4><a name="SLO">server_message = SLO</a> (power)</h4>
 * <p>
 * This command is sent from the server to the client, and indicates that the
 * game has ended due to a solo by the specified power. It is sent after the
 * <b>SCO</b> message and before the <b>NOW</b> message.
 * </p>
 */
public class SoloCommand extends Command {

	/**
	 * The power who won this game.
	 */
	private StandardToken winningPower;

	/**
	 * Construct a new <code>SoloCommand</code> from the tokens given.
	 * 
	 * @param tokens
	 *        The tokens to construct this from. It should be empty.
	 * @throws ParseException
	 *         Never.
	 */
	public SoloCommand(TokenGroup tokens) throws ParseException {
		super(tokens);
	}

	/**
	 * Construct a new <code>SoloCommand</code> informing the clients the
	 * given power has won.
	 * 
	 * @param winningPower
	 *        The winning power.
	 */
	public SoloCommand(StandardToken winningPower) {
		this.winningPower = winningPower;
	}

	@Override
	public byte getType() {
		return Commands.SLO;
	}

	@Override
	public String toString() {
		return winningPower.toString() + " has won the game!";
	}

	@Override
	public void doIO(TokenIO tokenIO, TokenGroup tokens) throws ParseException {
		winningPower = (StandardToken) tokenIO.doToken(tokenIO.subGroup(tokens), winningPower, TokenMap.powers);

	}

}
